﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThietBiSo.Models;
using ThietBiSo.LinQDB;

namespace ThietBiSo.Controllers
{
    public class onlineController : Controller
    {
        //
        // GET: /online/
        MOnline monl = new MOnline();
        Online onl = new Online();
        MLoaiOnline mlonl = new MLoaiOnline();
        public ActionResult Index()
        {
            ViewData["onlines"] = monl.FindAll();
            return View();
        }

        public ActionResult Create()
        {
            List<LoaiOnline> mdts = mlonl.FindAll();
            ViewData["loaionlines"] = mdts;
            return View();
        }

        //
        // POST: /ONLINE/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {

                // TODO: Add insert logic here

                onl.MaOnline = monl.GetMax() + 1;
                onl.TieuDe= collection.Get("TieuDe");
                onl.TenOnline = collection.Get("TenOnline");
                onl.MaLoaiOnline = int.Parse(collection.Get("ddlLoaiOnline"));
                onl.HienThi = false;
                if (monl.GetMaxTT() == 0 || monl.GetTotalOnline() == 0)
                {
                    onl.ThuTu = 1;
                }
                else
                    onl.ThuTu = monl.GetMaxTT() + 1;
                monl.Save(onl);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Edit(int id)
        {
            onl = monl.GetByID(id);
            ViewData["onlines"] = onl;
            List<LoaiOnline> llt = mlonl.FindAll();
            ViewData["loaionlines"] = llt;
            return View();
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {


                onl.MaOnline = id;
                onl.TieuDe = collection.Get("TieuDe");
                onl.TenOnline = collection.Get("TenOnline");
                onl.MaLoaiOnline = int.Parse(collection.Get("ddlLoaiOnline"));
                onl.ThuTu = int.Parse(collection.Get("ThuTu"));
                monl.Update(onl);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Delete(int id)
        {
            monl.Delete(id);
            return RedirectToAction("Index");
        }

    }
}
